<%--
  Created by IntelliJ IDEA.
  User: 86188
  Date: 2023/6/6
  Time: 11:20
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.jizhangben.bean.AccountListBean" %>
<%@ page import="com.jizhangben.pojo.AccountPojo" %>
<%@ page import="java.util.List" %>
<%-- 每页显示的条目数 --%>
<% int PAGE_SIZE = 7; %>

<%-- 当前页数 --%>
<%
    String pageNum0 = request.getParameter("page");
    int currentPage = 1;
    if (pageNum0 != null) {
        currentPage = Integer.parseInt(pageNum0);
    }
%>
<jsp:useBean id="accountListBean" class="com.jizhangben.bean.AccountListBean" scope="page"/>

<%
    // 获取 request 中传递的 accounts 属性，并将其设置为 accountListBean 的成员变量
    List<AccountPojo> accounts = (List<AccountPojo>) request.getAttribute("accounts");
    accountListBean.setAccounts(accounts);
    accountListBean.setCurrentPage(currentPage);
    accountListBean.setPageSize(PAGE_SIZE);
    accountListBean.setTotalPage(accounts);
%>
<html>
<head>
    <title>记事本主页</title>
    <link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>你好，${yourName}。</h1>
<h3 style="text-align: center"><a href="${pageContext.request.contextPath}/login">退出</a></h3>
<div style="text-align: center">
    <%-- 显示总页数 --%>
    共 ${accountListBean.totalPage} 页

    <%-- 显示每一页的链接 --%>
    <c:forEach begin="1" end="${accountListBean.totalPage}" var="page">
        <a href="${pageContext.request.contextPath}/account?page=${page}">${page}</a>
    </c:forEach>
</div>
<table>
    <caption align="left">记账信息</caption>
    <thead>
    <tr>
        <th>用途</th>
        <th>开销</th>
        <th>时间</th>
        <td>
            <a id="link" href="${pageContext.request.contextPath}/insert">增加</a>
        </td>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${accountListBean.currentPageAccounts}" var="ac">
        <tr class="td-center">
            <c:if test="${ac.name.length()<6}">
                <td class="td-center">${ac.name}</td>
            </c:if>
            <c:if test="${ac.name.length()>5}">
                <td class="td-center">${ac.name.substring(0,3)}...</td>
            </c:if>
            <td class="td-center">${ac.money}</td>
            <td class="td-center">${ac.time}</td>
            <td class="td-center"><a href="${pageContext.request.contextPath}/details?tableId=${ac.id}">详情</a></td>
        </tr>
    </c:forEach>
    </tbody>
    <tfoot>
    <tr>
        <td>
            <%-- 如果存在上一页，则显示上一页链接 --%>
            <c:if test="${accountListBean.hasPreviousPage}">
                <a href="${pageContext.request.contextPath}/account?page=${accountListBean.previousPage}">上一页</a>
            </c:if>
        </td>
        <td>
            <%-- 显示当前页数和总页数 --%>
            第 ${accountListBean.currentPage}/${accountListBean.totalPage} 页
        </td>
        <td>
            <%-- 如果存在下一页，则显示下一页链接 --%>
            <c:if test="${accountListBean.hasNextPage}">
                <a href="${pageContext.request.contextPath}/account?page=${accountListBean.nextPage}">下一页</a>
            </c:if>
        </td>
    </tr>
    </tfoot>
</table>
</body>
</html>
